<?php
namespace api\modules\mp\models;

use yii;
use yii\web\IdentityInterface;

class User extends yii\base\Model implements IdentityInterface {
    public $id;
    public $openid;

    public static function findIdentity($id) {
        // TODO: Implement findIdentity() method.
    }

    public static function build($data = []) {
        if (!empty($data['openid'])) {
            $one = Yii::$app->db->createCommand("SELECT * FROM auth_wechat WHERE mp_openid=:openid", [':openid' => $data['openid']])->queryOne();
            if (!$one) return null;
            $data = array_merge($one, $data);
        }
        return new self([
            'id' => $data['id'],
            'openid' => $data['openid']
        ]);
    }

    public static function findIdentityByAccessToken($token, $type = null) {
        $request = Yii::$app->request;

        if (!is_string($token)) {
            return null;
        }
        $key = $request->cookieValidationKey ?: '';
        $data = Yii::$app->getSecurity()->validateData($token, $key);
        if ($data === false) {
            return null;
        }
        $openid = $data;
        return self::build(['openid' => $openid]);
    }

    public function validateAuthKey($authKey) {
        // TODO: Implement validateAuthKey() method.
    }

    public function getAuthKey() {
        // TODO: Implement getAuthKey() method.
    }

    public function getId() {
        return $this->id;
    }
}
